$(function() {
    var cols=6;
	var rows_delete=0;
	var mode="add";
	var m_id=0;
    var url='controls/otherpay_model.php';
	 
	if (_dd==1){
		$("#btnAdd").show();
	}else{
		$("#btnAdd").hide();
	}
   	var checkDate = $('.btndate').datepicker().on('changeDate', function(ev) {
	  checkDate.hide();
	  $("#txtName").focus();
	}).data('datepicker');

	var checkDate2 = $('.btndate2').datepicker().on('changeDate', function(ev) {
	  checkDate2.hide();
	}).data('datepicker');
		
	$(".txtamt").keypress(function(e){
		
		if( e.which!=8 && e.which!=0 && (e.which<48 || e.which>57)){
			return false;
		}
	});
		 
	 SearchData();
 
	 function LoadData(month,name,type){
        $( "#resultData tbody tr" ).remove();
 
        
        $('#resultData').dataTable( {
			"bProcessing": true,
			"bFilter": false,
			"bLengthChange": false,
			"sAjaxSource": url,
			"oLanguage": {
				"sEmptyTable":     "ไม่พบข้อมูล"
			},
			"bDestroy": true,
			"sServerMethod": "POST",
			"fnServerParams": function ( aoData ) {
					aoData.push( { "name": "m", "value": "load"} ,
					{ "name": "month", "value": month} ,
					{ "name": "name", "value": name},
					{ "name": "type", "value": type});
				},
			"aoColumnDefs":[
				{  "bVisible": false , "aTargets": [ 1 ] },	
				{  "bVisible": false , "aTargets": [ 2 ] },	
				{   "bSortable": true, "aTargets": [ 3 ] ,"sClass":"text-center"},	
				{   "bSortable": true, "aTargets": [ 4 ] ,"sClass":"text-center"},	
				{   "bSortable": true, "aTargets": [ 5 ] ,"sClass":"text-left" },	
				{   "bSortable": true, "aTargets": [ 6 ]  ,"sClass":"text-right" },	
				{  "aTargets": [ 7 ],
					"sClass":"center",
					"mData": null,
					"mRender": function ( data, type, full ) {
						if (_it==1)
						return "<a href='#popup' data-toggle='modal'><img src='img/edit-file.png' class='btnEdit' style='cursor:pointer;'/></a>";
						else 
							return  "";
					}
				
				},	 
				 
				{"aTargets": [ 8 ],
					"sClass":"center",
					"mData": null,
					"mRender": function ( data, type, full ) {
						if (_te==1) 
						return "<a href='#popupConfirm' data-toggle='modal'><img src='img/delete-file.png' class='btnDel' style='cursor:pointer;'/></a>";
						else 
						return "";
					}
				}
			],"fnDrawCallback": function ( oSettings ) {
				 var that = this;
				/* Need to redo the counters if filtered or sorted */
                 var iTotalRecords = oSettings.fnRecordsTotal();
				//alert(oSettings.aiDisplay.length + ' ' + iTotalRecords);
				 
					if (iTotalRecords > 0){ 	
						if ( oSettings.bSorted || oSettings.bFiltered )
						{
							for ( var i=0, iLen=oSettings.aiDisplay.length ; i<iLen ; i++ )
							{
								$('td:eq(0)', oSettings.aoData[ oSettings.aiDisplay[i] ].nTr ).html( i+1 );
							}
						}
					}
					 
				} 
		 } );
		 
      }
 	
	 
	$("#btnSearch").click(function(){
		SearchData();
	});
	function SearchData(){
	var _month = $("#txtMonthSearch").val();
		var _name = $("#txtList").val();
		var _type = $("#cboPay_type_search").val();
		 
		LoadData(_month,_name,_type);
	}
	$(".btnEdit").live('click',function(){
		mode="edit";
		var rows = $('#resultData').dataTable().fnGetPosition($(this).closest("tr").get(0));
		var data =$('#resultData').dataTable().fnGetData(rows);
		 
		m_id = data[1];
		$("#txtMonth").val(data[3]);
		$("#cboPay_type").val(data[2]);
		$("#txtName").val(data[5]);
		$("#txtAmount").val(data[6]);
		 
	});
	$("#btnSave").live('click',function(){
		$("#txtMonth,#txtName, #txtAmount").jqBootstrapValidation({preventSubmit: false,
			submitSuccess: function ($form, event) { event.preventDefault();}
		}); 
	  
		var _month = $.trim($("#txtMonth").val());		
		var _name = $.trim($("#txtName").val());	
		var _amount = $("#txtAmount").val().replace(',','');
		var _type = $("#cboPay_type").val();
		var dataSet;
		 
		if (mode=="add"){
			dataSet={"m":"add","month":_month,"name":_name,"type":_type,"amount":_amount};  
		} else{
			dataSet={"m":"edit","id":m_id,"month":_month,"name":_name,"type":_type,"amount":_amount};  
		}
		if (_month!="" && _name !="" && _amount !=''){
			$.ajax({
				type: 'POST',
				cache: false,
				url: url,
				data: dataSet,
				success: function(data){
					$("#popup").modal('hide');
					 SearchData();
				 
				}
			});
			
		}
		 $("#txtMonth,#txtName, #txtAmount").jqBootstrapValidation("destroy");
	});		
	 
	$("#btnAdd").live('click', function(){
		mode="add";
		
	
		$("#txtMonth,#txtName, #txtAmount").val('');
	});
		
	//Popup confirm delete
	$(".btnDel").live('click',function(){
		var rows = $(this).closest("tr").get(0);
		rows_delete = $('#resultData').dataTable().fnGetPosition(rows);

	}); 
	
	$("#btnDelete").live('click', function(){
		var row_id =$('#resultData').dataTable().fnGetData(rows_delete);
		
		var id=row_id[1];
		 
		 $.ajax({
				 type: 'POST',
				 cache: false,
				 url: url,
				 data: {"m":"del","id":id},
				 success: function(data){
					$("#popupConfirm").modal('hide');
					$('#resultData').dataTable().fnDeleteRow( rows_delete );
					$('#resultData').dataTable().fnDraw();
				 }
			 });
 
	}); 
	
	 $("#btnPrintDraft").click(function(){
	  $("#resultData").printThis();
		return false;
           
	});	

    
});


